package com.shi.cjmoney;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qfhd.soft.cjzcore.CJZManager;

public class MainActivity extends AppCompatActivity {
    private Activity mActivity;
    private Context mContext;

    private TextView tip;
    private FrameLayout container;
    private Button btn_open;

    private boolean isInit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mActivity = this;
        mContext = getApplicationContext();

        tip = (TextView)findViewById(R.id.txt);
        container = (FrameLayout)findViewById(R.id.container);
        btn_open = (Button)findViewById(R.id.main_open);
        btn_open.setOnClickListener(v -> {
            if(isInit) {
                CJActivityManager.openActivity(mActivity, com.qfhd.soft.cjzcore.UI.MainActivity.class);
            }
        });

        CJPermission.checkAndRequestPermission(getApplicationContext(),mActivity,()->{
            loadData();
        });
    }

    private void loadData(){
        new Thread(()->{
            CJZManager.getInstance().applicatonInit(getApplicationContext(),"1110327936","5038180");
            boolean ret = CJZManager.getInstance().initData(getApplicationContext(),this,"d0491b5197125611b9bcec46b9602c77");
            isInit = ret;
            if(isInit) {
                mActivity.runOnUiThread(()->{
                    tip.setText("初始化成功");
                });
            }
            else{
                mActivity.runOnUiThread(()->{
                    tip.setText("初始化失败");
                });
            }
        }).start();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1024 && hasAllPermissionsGranted(grantResults)) {
            Log.v("CJZCore","get permission");
//            fetchSplashAD(this, container, skipView, Constants.APPID, getPosId(), this, 0);
            loadData();
        } else {
            Toast.makeText(this, "应用缺少必要的权限！请点击\"权限\"，打开所需要的权限。", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
            finish();
        }
    }

    private boolean hasAllPermissionsGranted(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }

}
